package com.mida.app.report.service.impl;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.mida.app.report.service.DocStructDefinitionSpaceService;
import com.mida.app.report.service.MdmService;
import com.mida.bms.report.api.DocDefineSpaceConsumerApi;
import com.mida.bms.report.api.DocStructDefinitionSpaceApi;
import com.mida.bms.report.entity.DocStructDefinitionSpace;
import com.mida.bms.report.vo.DocDefineSpaceConsumerVo;
import com.mida.bms.report.vo.DocStructDefinitionSpaceVo;
import com.mida.dms.mdm.dto.UserDto;
import com.mida.dms.mdm.vo.provider.BusinessUnitVo;
import com.mida.dms.mdm.vo.provider.EmployeeInfoVo;
import com.mida.tool.common.result.ListResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.StopWatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.mida.tool.common.result.BaseResult.buildListSuccess;
import static com.mida.tool.common.result.BaseResult.buildListWarn;

/**
 * @ClassName: DocStructDefinitionSpaceServiceImpl
 * @Description: 结构化模板定义空间Impl
 * @Author: wang xiao le
 * @Date: 2021/6/18 19:16
 **/
@Service
@Slf4j
@Transactional
public class DocStructDefinitionSpaceServiceImpl implements DocStructDefinitionSpaceService {

    private static final String LONG_STRING = "|";
    private static final String COMMA_STRING = ",";

    /**
     * 结构化模板定义空间feign接口
     */
    @Autowired
    private DocStructDefinitionSpaceApi docStructDefinitionSpaceApi;

    @Autowired
    private DocDefineSpaceConsumerApi docDefineSpaceConsumerApi;



    @Autowired
    private MdmService mdmService;
    /**
     * 4、获取单个定义空间
     *
     * @param defineSpaceSoid 定义空间soid
     * @return DocStructDefinitionSpaceVo
     */
    @Override
    public ListResult<DocStructDefinitionSpaceVo> gainDocStructDefinitionSpaceVo(long defineSpaceSoid) {
        log.info("开始获取定义空间:" + defineSpaceSoid);
        StopWatch stopWatch = StopWatch.createStarted();
        ListResult<DocStructDefinitionSpaceVo> listResult = new ListResult<>();

        try {
            //获取定义空间
            ListResult<DocStructDefinitionSpaceVo> docStructDefinitionSpaceVoListResult =
                    docStructDefinitionSpaceApi.selectByDefineSpaceSoid(defineSpaceSoid);
            DocStructDefinitionSpaceVo docStructDefinitionSpaceVo =
                    docStructDefinitionSpaceVoListResult.getData();
            ListResult<List<DocDefineSpaceConsumerVo>> docDefineSpaceConsumerVoListResult =
                    docDefineSpaceConsumerApi.selectConsumersByDefineSpaceSoid(defineSpaceSoid);
            List<DocDefineSpaceConsumerVo> docDefineSpaceConsumers =
                    docDefineSpaceConsumerVoListResult.getData();

            //设置医生姓名、以及医生姓名与业务单元拼接的字段
            setDocDefineSpaceConsumerList(docStructDefinitionSpaceVo, docDefineSpaceConsumers);

            buildListSuccess(listResult, docStructDefinitionSpaceVo);
        } catch (Exception e) {
            log.error("获取单个定义空间发送异常", e);
            buildListWarn(listResult, "获取定义空间失败");
        } finally {
            stopWatch.stop();
            log.info("本次查询耗时:" + stopWatch.getTime() + "ms");
        }
        return listResult;
    }

    /**
     * 设置医生姓名、以及医生姓名与业务单元拼接的字段
     * @param docStructDefinitionSpaceVo
     * @param docDefineSpaceConsumers
     */
    private void setDocDefineSpaceConsumerList(
            DocStructDefinitionSpaceVo docStructDefinitionSpaceVo,
            List<DocDefineSpaceConsumerVo> docDefineSpaceConsumers) {

        if (ObjectUtils.isNotEmpty(docDefineSpaceConsumers)) {
            docDefineSpaceConsumers.forEach(consumer ->{
                ListResult<EmployeeInfoVo> employee = mdmService.findOneBySoid(
                        consumer.getDoctorSoid(), false);

                //设置医生姓名
                String doctorName  = employee.getData().getEmployeeName();
                consumer.setDoctorName(doctorName);

                //设置医生姓名与业务单元拼接的字段
                ListResult<List<BusinessUnitVo>> unitList =
                        mdmService.findBusinessUnitListByEmployeeSoid(
                                consumer.getDoctorSoid(),
                                null);

                StringBuffer doctorNameWithUnitName =
                        new StringBuffer(doctorName + LONG_STRING);

                List<BusinessUnitVo> data = unitList.getData();
                for (int i = 0; i < data.size(); i++) {
                    if(i == data.size() - 1){
                        doctorNameWithUnitName.append(
                                data.get(i).getBusinessUnitName());
                    }else {
                        doctorNameWithUnitName.append(
                                data.get(i).getBusinessUnitName() + COMMA_STRING);
                    }
                }
                consumer.setDoctorNameWithUnitName(doctorNameWithUnitName.toString());
            });
        }

        docStructDefinitionSpaceVo.setDocDefineSpaceConsumerList(docDefineSpaceConsumers);
    }

    @Override
    public ListResult<List<DocStructDefinitionSpace>> findAll(String searchData) throws Exception{
        ListResult<UserDto> user = mdmService.getLoginUserInfo();
        Long hospitalSoid = null;
        if (ObjectUtils.isNotEmpty(user.getData())) {
            hospitalSoid = user.getData().getHospitalSoid();
        }
        return docStructDefinitionSpaceApi.findAll(searchData,hospitalSoid);
    }
}
